package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.StrategyCommend;
import cn.wolfcode.luowowo.article.mapper.StrategyCommendMapper;
import cn.wolfcode.luowowo.article.query.StrategyCommendQuery;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * Created by admin on 2019/8/12.
 */
@Service
public class StrategyCommendServiceImpl implements IStrategyCommendService {

    @Autowired
    private StrategyCommendMapper commendMapper;

    @Override
    public PageInfo<StrategyCommend> query(StrategyCommendQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo<>(commendMapper.seleceForList(qo));
    }

    @Override
    public void saveOrUpdate(StrategyCommend strategyCommend) {

        if (strategyCommend.getSequence() == null) {
            int max = commendMapper.selectMaxSequence(strategyCommend.getStrategyId());
            strategyCommend.setSequence(max + 1);
        }

        if (strategyCommend.getId() == null) {
            commendMapper.insert(strategyCommend);
        } else {
            commendMapper.updateByPrimaryKey(strategyCommend);
        }
    }

    @Override
    public List<StrategyCommend> selectCommendTop5() {
        return commendMapper.selectCommendTop5();
    }

}
